package com.caiyi.financial.nirvana.sms.mapper;

import com.caiyi.financial.nirvana.sms.base.mapper.BaseMapper;
import com.caiyi.financial.nirvana.sms.querybean.QueryAccountBlacklistBean;
import com.caiyi.financial.nirvana.sms.entity.SmsAccountBlacklist;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface SmsAccountBlacklistMapper extends BaseMapper<SmsAccountBlacklist> {

    @SelectProvider(type = AccountBlacklistSqlProvider.class, method = "selectAccountBlacklistListByParams")
    List<SmsAccountBlacklist> selectAccountBlacklistListByParams(QueryAccountBlacklistBean bean);
    @Select("select count(id) from sms_account_blacklist where account = #{account} and phone = #{phone}")
    int selectAccountBlacklist(SmsAccountBlacklist smsAccountBlacklist);

    class AccountBlacklistSqlProvider {
        public String selectAccountBlacklistListByParams(QueryAccountBlacklistBean bean) {
            return new SQL() {
                {
                    SELECT("id,account,phone,source,create_time,update_time ");
                    FROM("sms_account_blacklist");
                    if (!StringUtils.isEmpty(bean.getAccount())) {
                        WHERE("account LIKE concat('%',#{account},'%')");
                    }
                    if (!StringUtils.isEmpty(bean.getPhone())) {
                        WHERE("phone LIKE concat('%',#{phone},'%')");
                    }
                    if(bean.getSource()!=null){
                        WHERE("source=#{source}");
                    }
                    if (bean.getStartTime() != null) {
                        WHERE("create_time >= #{startTime}");
                    }
                    if (bean.getEndTime() != null) {
                        WHERE("create_time <= date_add(#{endTime}, interval 1 day)");
                    }
                    ORDER_BY("id desc");

                }
            }.toString();
        }



    }
}
